package cn.myjerry.test.ORM;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Demo04 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		PreparedStatement ps1 = null;
		ResultSet res = null;
		ResultSet res1 = null;
		List<Employee> list = new ArrayList<>();
		
		try {
			conn = JDBCUtil.getMysqlConn();
			ps = conn.prepareStatement("SELECT * FROM Employee");
			res = ps.executeQuery();
			while (res.next()) {
				Employee emp = new Employee(res.getInt("Id"), res.getString("Name"), res.getDouble("Salary"), res.getInt("Age"), res.getDate("Time"), res.getInt("DepartmentId"));
				ps1 = conn.prepareStatement("SELECT * FROM Department WHERE Id = ?");
				ps1.setInt(1, res.getInt("DepartmentId"));
				res1 = ps1.executeQuery();
				while(res1.next()) {
					Department department = new Department(res1.getInt("Id"), res1.getString("Name"), res1.getString("Address"));
					emp.setDepartment(department);
					list.add(emp);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(res, ps, conn);
		}
		for(Employee emp:list) {
			System.out.println(emp.toString());
		}
	}
}
